/**
 * @file   TTF001Msg.java
 * @date   2012-11-12上午11:25:10
 * @author User
 */
package com.gzceb.zhgjj.model;

import java.io.File;
import java.lang.reflect.Constructor;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.gzceb.zhgjj.filecheck.AbstractFileCheck;
import com.gzceb.zhgjj.filecheck.FileCheck;
import com.gzceb.zhgjj.filecheck.impl.LoanAprvCheck;
import com.gzceb.zhgjj.fileload.AbstractFileLoad;
import com.gzceb.zhgjj.fileload.FileLoad;
import com.gzceb.zhgjj.socket.SocketClientTTF001;
import com.gzceb.zhgjj.util.PropertiesUtils;
import com.gzceb.zhgjj.util.Utils;
import com.gzceb.zhgjj.util.XMLUtils;

/**
 * <p>TTF001报文处理类。</p>
 *
 * @date   2012-11-12上午11:25:10 
 * @author liqing
 * @description 
 */
public class TTF001Msg extends AbstractMsg {
	private  final Log logger = LogFactory.getLog(TTF001Msg.class);
	
	public String destPath;
	
	public TTF001Msg(String destPath){
		this.destPath = destPath;
	}
	
	/**
	 * 处理中心发来的TTF001报文			
	 * @auth   		liqing
	 * @date 	Nov 12, 2012 4:32:25 PM 
	 * @param message 中心请求TTF001报文
	 * 
	 */
	public String ProcRcvMsg(String message){
		String rspStr="";
		logger.info("got the message: " + "\n" + message);
		Map<String, String> msgMap = XMLUtils.XMLParser(message.toString());
	
		File file = new File(this.destPath + msgMap.get("File_Name"));
		
		// 文件校验，并生成响应文件
		 Class demo ;
	     String clsname = "com.gzceb.zhgjj.filecheck.impl." 
					+ PropertiesUtils.getValue(msgMap.get("File_Type"));  
	     FileCheck fileCheck=null; 
	     String rspFileName = "";
	    	try{
	    		demo=Class.forName(clsname);
	    		Constructor<?> cons = demo.getConstructor(File.class);
		    	fileCheck = (FileCheck)cons.newInstance(file);
		    	rspFileName = createRspFile(fileCheck);
		    	//向中心7661端口发送响应文件的处理 SocketClientTTF001
				SocketClientTTF001.service(rspFileName);
	    	}catch(IllegalArgumentException ie){
	    		logger.error(ie.getMessage());
	    	}catch(NoSuchMethodException ne){
	    		logger.error(ne.getMessage());
	    	}
	    	catch(Exception e){
	    		logger.error("error happended when create Class. \n" + e.getMessage());
	    		e.printStackTrace();
	    	}

	    if( !"".equals(rspFileName) ){
	    	// 根据响应文件名，调用接口，另起线程完成文件内容入库	
	    	Class demo2;
	    	String clsname2 = "com.gzceb.zhgjj.fileload.impl."
	    			+ PropertiesUtils.getValue("FL_" + rspFileName.substring(0, 2));
	    	AbstractFileLoad abFileLoad=null;
	    	try{
	    		demo2 = Class.forName(clsname2);
	    		Constructor<?> cons = demo2.getConstructor(String.class);
	    		abFileLoad = (AbstractFileLoad) cons.newInstance(this.destPath + rspFileName);
	    		
	    	} catch(IllegalArgumentException ie){
	    		logger.error(ie.getMessage());
	    	} catch(NoSuchMethodException ne){
	    		logger.error(ne.getMessage());
	    	}
	    	catch(Exception e){
	    		logger.error("error happended when create Class. \n" + e.getMessage());
	    		e.printStackTrace();
	    	}
	    	
	    	new Thread(abFileLoad).start();
	    }
	    	
		rspStr = createRspMsg(rspFileName);
		return rspStr;
	}
	
	/**
	 * 生成TTF001响应文件			
	 * @auth   		liqing
	 * @date 	Nov 12, 2012 4:38:25 PM 
	 * @param fileCheck 文件校验接口，根据接收报文中的File_Type对应不同校验类接口
	 * @return fileName 文件名包含绝对路径
	 */
	public String createRspFile(FileCheck fileCheck){
		
		return fileCheck.crtRespFile();
	}
	
	/**
	 * 生成TTF001响应报文			
	 * @auth   		liqing
	 * @date 	Nov 12, 2012 4:40:25 PM 
	 * @param 
	 * 
	 */
	public String createRspMsg(String rspFileName){
		//TODO 需增加成功标志的判断
		String sucFlag;
		if("".equals(rspFileName) && rspFileName.length() == 0){
			sucFlag = Constants.SUC_FLAG_FAIL;
		}else{
			sucFlag = Constants.SUC_FLAG_SUC;
		}
		//TODO 需增加响应代码的判断
		String respCode = Constants.RESPCODE_SUC;
		
		String rspStr = "<message>"
				+"<head>"
				+"<Tx_code>" + Constants.TXCODE_TTF001 + "</Tx_code>"
				+"<BankId>" + Constants.BANKID + "</BankId>"
				+"<TallyDate>" + Utils.getCurrDateStr("yyyyHHdd") + "</TallyDate>"
				+"<Spe_jnl_no></Spe_jnl_no>"
				+"<VoucherNum></VoucherNum>"
				+"<Suc_flag>" + sucFlag + "</Suc_flag>"
				+"<RespCode>" + respCode + "</RespCode>"
				+"<RespMess>Successful</RespMess>"
				+"<Servertype></Servertype>"
				+"</head>"
				+"<body>"
				+"<File_Type>" + Utils.getFileType(rspFileName) + "</File_Type>"
				+"<File_Name>" + rspFileName + "</File_Name>"
				+"</body>"
				+"</message>";
		
		return Utils.intToString(rspStr.length(), 4)+ rspStr;
	}
	
	/*
	public static void test(String rspFileName){
		if( !"".equals(rspFileName) ){
	    	// 根据响应文件名，调用接口，另起线程完成文件内容入库	
	    	Class demo2;
	    	String clsname2 = "com.gzceb.zhgjj.fileload.impl."
	    			+ PropertiesUtils.getValue("FL_" + rspFileName.substring(0, 2));
	    	AbstractFileLoad abFileLoad=null;
	    	try{
	    		demo2 = Class.forName(clsname2);
	    		Constructor<?> cons = demo2.getConstructor(String.class);
	    		abFileLoad = (AbstractFileLoad) cons.newInstance(rspFileName);
	    		
	    	} catch(IllegalArgumentException ie){
	    		ie.printStackTrace();
	    	} catch(NoSuchMethodException ne){
	    		ne.printStackTrace();
	    	}
	    	catch(Exception e){
	    		e.printStackTrace();
	    	}
	    	
	    	new Thread(abFileLoad).start();
	    }
	}
	*/
	
	public static void main(String[] args){
		ApplicationContext ac = new ClassPathXmlApplicationContext("/spring/applicationContext.xml");
		
		//test("LD23232012212332341");
	}

}
